﻿using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Foundation.ExtensionMethods;
using Foundation.MarkupExtensions;

namespace Poet.Resources
{
    /// <summary>
    /// Interaction logic for DataProviders.xaml
    /// </summary>
    public partial class DataProviders 
    {
        public DataProviders()
        {
            InitializeComponent();
        }

        public static List<EncodingInfo> AllEncodingInfos = Encoding.GetEncodings().ToList();

        public object GetEncodingMenuReloadWith()
        {
            var menu = GetBaseEncodingMenu(ApplicationCommands.Open);
            ((MenuItem) menu[0]).InputGestureText = "Ctrl+Shift+R";
            return menu;
        }

        public object GetEncodingMenuUseForSave()
        {
            var menu = GetBaseEncodingMenu(ApplicationCommands.Properties);
            ((MenuItem)menu[0]).InputGestureText = "Ctrl+E";
            return menu;
        }

        private static List<Control> GetBaseEncodingMenu(RoutedCommand command)
        {
            var pattern = Encoding.Default.EncodingName.Split(new[] { ' ', ',', '.' })[0];
            var encodings =
                new List<Encoding>(
                    AllEncodingInfos.Where(i => i.DisplayName.Contains(pattern)).Select(i => i.GetEncoding()))
                    {
                        Encoding.Default,
                        Encoding.ASCII,
                        Encoding.BigEndianUnicode,
                        Encoding.UTF32,
                        Encoding.UTF7,
                        Encoding.UTF8,
                        Encoding.Unicode,
                    }.Distinct().ToList();

            encodings.Sort((x, y) => string.CompareOrdinal(x.EncodingName, y.EncodingName));
            encodings.Remove(Encoding.Default);
            var menu = encodings.Select(e => new MenuItem { Header = e.EncodingName, DataContext = e }).Cast<Control>().ToList();
            menu.Insert(0, new Separator());
            menu.Insert(0, new MenuItem { Header = Encoding.Default.EncodingName, DataContext = Encoding.Default });

            menu.Insert(0, new Separator());
            menu.Insert(0, new MenuItem
                {
                    Command = command,
                    CommandParameter = ":Encoding",
                    InputGestureText = " ", 
                    Header = Localizing.ProvideValue("Custom"),
                });
          
            foreach (var menuItem in menu.Where(i => i.DataContext is Encoding).Cast<MenuItem>())
            {
                menuItem.Command = command;
                menuItem.CommandParameter = menuItem.DataContext;
                menuItem.StaysOpenOnClick = true;
                menuItem.InputGestureText = " ";
            }

            return menu;
        }

        public List<double> GetDefaultFontSizes()
        {
            var sizeList = new List<double>();
            for (var i = 1; i < 100; i++)
            {
                sizeList.Add(i);
            }
            return sizeList;
        }

        public List<FontWeight> GetDefaultFontWeights()
        {
            return new List<FontWeight>
                {
                    FontWeights.Thin,
                    FontWeights.ExtraLight,
                    FontWeights.Light,
                    FontWeights.Normal,
                    FontWeights.Medium,
                    FontWeights.SemiBold,
                    FontWeights.Bold,
                    FontWeights.ExtraBold,
                    FontWeights.Black,
                    FontWeights.ExtraBlack,
                };
        }

        public List<FontStyle> GetDefaultFontStyles()
        {
            return new List<FontStyle>
                {
                    FontStyles.Normal,
                    FontStyles.Italic,
                    FontStyles.Oblique,
                };
        }

        public List<FontStretch> GetDefaultFontStretches()
        {
            var stretchList =
                typeof(FontStretches).GetProperties().ToList().Select(
                    p => p.GetValue(null, BindingFlags.Public, null, null, null)).OfType<FontStretch>().ToList();
            var medium = stretchList.Find(s => s.ToString() == "Medium");
            stretchList.Remove(medium);
            return stretchList;
        }
    }
}
